package com.htfyun.ft.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.text.TextUtils;

import com.htfyun.ft.R;
import com.htfyun.ft.utils.FileReadWrite;


/**
 * Created by sst on 2018/7/7.
 */

public class BatteryInfo extends BroadcastReceiver{

    private final Context mContext;

    private int batteryTemperature = 0;
    private int batteryLevel = 0;
    private int batteryVoltage = 0;
    private String batteryStatusString = "";
    private int batteryStatus = -1;
    private String batteryChargePlugTypeString = "";
    private int batteryChargePlugType = -1;
    private String batteryHealthString = "";
    private int batteryHealth = -1;

    private static final String CURRENT_NOW_PATH = "/sys/class/power_supply/battery/current_now";

    private OnBatteryChangedListener mListener;

    private volatile boolean isRegistered = false;

    public BatteryInfo(Context ctx) {
        mContext = ctx;
    }

    public synchronized void startMonitor() {
        if (isRegistered) {
            return;
        }
        isRegistered = true;
        mContext.registerReceiver(this, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
    }

    public void stopMonitor() {
        if (!isRegistered) {
            return;
        }
        isRegistered = false;

        mContext.unregisterReceiver(this);
    }

    public void setOnBatteryChangedListener(OnBatteryChangedListener listener) {
        this.mListener = listener;
    }

    public int getBatteryTemperature() {
        return batteryTemperature;
    }

    public int getBatteryLevel() {
        return batteryLevel;
    }

    public int getBatteryVoltage() {
        return batteryVoltage;
    }

    public String getBatteryStatusString() {
        return batteryStatusString;
    }

    public String getBatteryChargePlugTypeString() {
        return batteryChargePlugTypeString;
    }

    public String getBatteryHealthString() {
        return batteryHealthString;
    }

    public int getBatteryStatus() {
        return batteryStatus;
    }

    public int getBatteryChargePlugType() {
        return batteryChargePlugType;
    }

    public int getBatteryHealth() {
        return batteryHealth;
    }

    public int getBatteryCurrent_mA() {

        int batteryCurrent_mA = -1;
        try {
            String currentStr = FileReadWrite.readFileByLines(CURRENT_NOW_PATH);
            if (!TextUtils.isEmpty(currentStr)) {
                int current = Integer.decode(currentStr);
                batteryCurrent_mA = current / 1000;
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }

        return batteryCurrent_mA;

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == null) {
            return;
        }
        if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
            return;
        }
        batteryVoltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

        batteryTemperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);

        int level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
        int scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
        batteryLevel = (int)(((float)level / scale) * 100);

        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
        batteryStatus = status;
        int statusResId = R.string.battery_status_unknown;
        switch (status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                statusResId = R.string.battery_status_charging;
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                statusResId = R.string.battery_status_discharging;
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                statusResId = R.string.battery_status_not_charging;
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                statusResId = R.string.battery_status_full;
                break;
        }
        batteryStatusString = mContext.getResources().getString(statusResId);

        // 怎么充
        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        batteryChargePlugType = chargePlug;
        int chargePlugResId = R.string.battery_plugged_unknown;
        switch (chargePlug) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                chargePlugResId = R.string.battery_plugged_ac;
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                chargePlugResId = R.string.battery_plugged_usb;
                break;
            case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                chargePlugResId = R.string.battery_plugged_wireless;
                break;
        }
        batteryChargePlugTypeString = mContext.getResources().getString(chargePlugResId);

        int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN);
        batteryHealth = health;
        int healthResId = R.string.battery_health_unknown;
        switch (health) {
            case BatteryManager.BATTERY_HEALTH_GOOD:
                healthResId = R.string.battery_health_good;
                break;
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                healthResId = R.string.battery_health_overheat;
                break;
            case BatteryManager.BATTERY_HEALTH_DEAD: // 未充电时就会显示此状态，这是什么鬼？
                healthResId = R.string.battery_health_dead;
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                healthResId = R.string.battery_health_over_voltage;
                break;
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                healthResId = R.string.battery_health_unknown;
                break;
            case BatteryManager.BATTERY_HEALTH_COLD:
                healthResId = R.string.battery_health_cold;
                break;
        }
        batteryHealthString = mContext.getString(healthResId);

        if (mListener != null) {
            mListener.onBatteryChangedListener(BatteryInfo.this);
        }
    }

    public interface OnBatteryChangedListener {
        void onBatteryChangedListener(BatteryInfo info);
    }

}
